﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;

public class MeleeWeapon : Weapon, IMissileDefender
{
    public bool canHitMissile = true;
    private Coroutine continuallyApplyDamage;
    private Collider[] enteredColliders = new Collider[10];
    public float performDuration = 0.2f;
    public float prepareDuration = 0.1f;
    public Color32 trailColor = Color.white;
    private TronTrail trailEffect;
    public float trailLength = 0.8f;
    private Coroutine useCoroutine;

    [DebuggerHidden]
    private IEnumerator _Use()
    {
        return new <_Use>c__Iterator6 { <>f__this = this };
    }

    private void ApplyDamage()
    {
        if (base.isActiveAndEnabled)
        {
            Vector3 center = base.transform.TransformPoint((Vector3) (((Vector3.forward * base.attackRange) / 2f) + (Vector3.up * 0.5f)));
            Vector3 vector2 = new Vector3(base.attackRange, 1f, base.attackRange);
            int num = Physics.OverlapBoxNonAlloc(center, (Vector3) (vector2 / 2f), this.enteredColliders, base.transform.rotation, (int) base.targetLayerMask);
            for (int i = 0; i < num; i++)
            {
                if (!base.isActiveAndEnabled)
                {
                    break;
                }
                Collider co = this.enteredColliders[i];
                Character target = co.GetCharacter();
                if ((base.user == null) || (target != base.user))
                {
                    Vector3 hitPoint = co.bounds.center;
                    Vector3 hitNormal = base.transform.position - co.bounds.center;
                    this.Hit(base.user, target, hitPoint, hitNormal);
                }
            }
        }
    }

    protected override void Awake()
    {
        base.Awake();
        if ((base.modelInstance != null) && (this.trailLength > 0f))
        {
            this.trailEffect = new GameObject("MeleeWeaponTrailEffect").AddComponent<TronTrail>();
            this.trailEffect.gameObject.AddComponent<MeshRenderer>().material = new Material(Shader.Find("Sprites/Default"));
            this.trailEffect.transform.SetParentAndResetTransform(base.modelInstance);
            this.trailEffect.gameObject.SetActive(false);
        }
        base.targetLayerMask = (LayerMask) GameLayers.Character;
    }

    [DebuggerHidden]
    private IEnumerator ContinuallyApplyDamage()
    {
        return new <ContinuallyApplyDamage>c__Iterator7 { <>f__this = this };
    }

    bool IMissileDefender.Defence(Missile missile, RaycastHit hit)
    {
        if ((((hit.collider != null) && (hit.collider.GetCharacter() == base.user)) && ((this.trailEffect != null) && this.trailEffect.gameObject.activeSelf)) && (Vector3.Dot(hit.normal, base.transform.forward) > 0f))
        {
            missile.velocity = (Vector3) (hit.normal * missile.velocity.magnitude);
            return true;
        }
        return false;
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    protected override void OnUse(Character user)
    {
        base.OnUse(user);
        this.useCoroutine.Stop(this);
        this.useCoroutine = base.StartCoroutine(this._Use());
    }

    private void UNetVersion()
    {
    }

    [CompilerGenerated]
    private sealed class <_Use>c__Iterator6 : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal MeleeWeapon <>f__this;

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    if (this.<>f__this.trailEffect != null)
                    {
                        this.<>f__this.trailEffect.gameObject.SetActive(false);
                    }
                    this.$current = new WaitForSeconds(this.<>f__this.prepareDuration);
                    this.$PC = 1;
                    goto Label_01BD;

                case 1:
                    if (this.<>f__this.trailEffect != null)
                    {
                        this.<>f__this.trailEffect.height = this.<>f__this.trailLength;
                        this.<>f__this.trailEffect.startColor = (Color) this.<>f__this.trailColor;
                        this.<>f__this.trailEffect.endColor = (Color) this.<>f__this.trailColor;
                        this.<>f__this.trailEffect.gameObject.SetActive(true);
                    }
                    this.<>f__this.ConsumeUserEnergy();
                    this.<>f__this.ApplyDamage();
                    this.<>f__this.continuallyApplyDamage.Stop(this.<>f__this);
                    this.<>f__this.continuallyApplyDamage = this.<>f__this.ContinuallyApplyDamage().Start(this.<>f__this);
                    this.$current = new WaitForSeconds(this.<>f__this.performDuration);
                    this.$PC = 2;
                    goto Label_01BD;

                case 2:
                    this.<>f__this.continuallyApplyDamage.Stop(this.<>f__this);
                    if (this.<>f__this.trailEffect != null)
                    {
                        this.<>f__this.trailEffect.gameObject.SetActive(false);
                    }
                    this.<>f__this.useCoroutine = null;
                    this.$PC = -1;
                    break;
            }
            return false;
        Label_01BD:
            return true;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }

    [CompilerGenerated]
    private sealed class <ContinuallyApplyDamage>c__Iterator7 : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        internal MeleeWeapon <>f__this;

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    break;

                case 1:
                    this.<>f__this.ApplyDamage();
                    break;

                default:
                    goto Label_0054;
            }
            this.$current = new WaitForSeconds(0.5f);
            this.$PC = 1;
            return true;
            this.$PC = -1;
        Label_0054:
            return false;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }
}

